package com.chixing.service.impl;

import com.chixing.mapper.*;
import com.chixing.pojo.*;
import com.chixing.service.MyorderService;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Service
public class MyorderServiceImpl implements MyorderService {
    @Autowired
    private MyorderMapper myorderMapper;

    @Autowired
    private TravelSightsMapper travelSightsMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private MyorderTravelerMapper myorderTravelerMapper;

    @Autowired
    private FlowMapper flowMapper;
    @Autowired
    private SightsReviewMapper sightsReviewMapper;

    @Override  //根据订单Id查询订单详情
    public ServerResponse getByOrderId(Integer orderId) {
        MyorderTravelCustomerTravelerInfoVo myorderTravelCustomerTravelerInfoVo = new MyorderTravelCustomerTravelerInfoVo();
        Myorder myorder = myorderMapper.selectByPrimaryKey(orderId);//订单对象
        TravelSights travelSights = travelSightsMapper.selectByPrimaryKey(myorder.getTravelSightsId());//景点对象
        Customer customer = customerMapper.selectByPrimaryKey(myorder.getCustId());//用户对象

        MyorderTravelerExample myorderTravelerExample = new MyorderTravelerExample();
        MyorderTravelerExample.Criteria criteria = myorderTravelerExample.createCriteria();
        criteria.andMyorderIdEqualTo(myorder.getMyorderId()).andMyorderTravelerStatusEqualTo(1);
        List<MyorderTraveler> myorderTravelers = myorderTravelerMapper.selectByExample(myorderTravelerExample);

        myorderTravelCustomerTravelerInfoVo.setMyorderTravelerList(myorderTravelers);
        myorderTravelCustomerTravelerInfoVo.setCustomer(customer);
        myorderTravelCustomerTravelerInfoVo.setMyorder(myorder);
        myorderTravelCustomerTravelerInfoVo.setTravelSights(travelSights);
        myorderTravelCustomerTravelerInfoVo.setMyorderTravelerList(myorderTravelers);

        return ServerResponse.getSuccess(myorderTravelCustomerTravelerInfoVo);
    }

    @Override//渲染到订单详情页面
    public ServerResponse getByOrderNo(String orderNo) {
        MyorderTravelCustomerTravelerInfoVo myorderTravelCustomerTravelerInfoVo = new MyorderTravelCustomerTravelerInfoVo();
        MyorderExample myorderExample = new MyorderExample();
        myorderExample.createCriteria().andMyorderNoEqualTo(orderNo);
        Myorder myorder = myorderMapper.selectByExample(myorderExample).get(0);

        TravelSights travelSights = travelSightsMapper.selectByPrimaryKey(myorder.getTravelSightsId());

        Customer customer = customerMapper.selectByPrimaryKey(myorder.getCustId());

        MyorderTravelerExample myorderTravelerExample = new MyorderTravelerExample();
        myorderTravelerExample.createCriteria().andMyorderIdEqualTo(myorder.getMyorderId()).andMyorderTravelerStatusEqualTo(1);
        List<MyorderTraveler> myorderTravelerList = myorderTravelerMapper.selectByExample(myorderTravelerExample);

        myorderTravelCustomerTravelerInfoVo.setMyorder(myorder);
        myorderTravelCustomerTravelerInfoVo.setTravelSights(travelSights);
        myorderTravelCustomerTravelerInfoVo.setCustomer(customer);
        myorderTravelCustomerTravelerInfoVo.setMyorderTravelerList(myorderTravelerList);

        return ServerResponse.getSuccess(myorderTravelCustomerTravelerInfoVo);
    }

    //订单查询
    @Override
    public ServerResponse getByCustId(Integer custId,Integer myorderStatus) {
        MyorderExample myorderExample = new MyorderExample();
        MyorderExample.Criteria myorderExampleCriteria = myorderExample.createCriteria();
        if (myorderStatus == 5){
            myorderExampleCriteria.andCustIdEqualTo(custId).andStatusEqualTo(0);
        }else {
            myorderExampleCriteria.andCustIdEqualTo(custId).andMyorderStatusEqualTo(myorderStatus).andStatusEqualTo(0);
        }
        myorderExample.setOrderByClause("myorder_date desc");
        List<Myorder> myorders = myorderMapper.selectByExample(myorderExample);

        List<MyorderTravelVO> myorderTravelVOList = new ArrayList<>();
        for (Myorder myorder:myorders) {
            TravelSights travelSights = travelSightsMapper.selectByPrimaryKey(myorder.getTravelSightsId());
            MyorderTravelVO myorderTravelVO = new MyorderTravelVO();
            myorderTravelVO.setMyorder(myorder);
            myorderTravelVO.setTravelSights(travelSights);
            myorderTravelVOList.add(myorderTravelVO);
        }

        if (!(myorders.isEmpty())){
            return ServerResponse.getSuccess(myorderTravelVOList);
        }else {
            return ServerResponse.getFail(myorderTravelVOList);
        }
    }

    //添加订单
    @Override
    public Myorder createOrder(String custName, Long custTel, String travelName, Integer itineraryId, Integer audltNum, Integer childNum, Date myorderDate , Date myorderStartDate, Float myorderPrice) {
        Myorder myorder = new Myorder();
        //获取用户对象
        CustomerExample customerExample = new CustomerExample();
        customerExample.createCriteria().andCustNameEqualTo(custName).andCustTelnoEqualTo(custTel);
//        Customer customer = customerMapper.selectByExample(customerExample).get(0);
        Customer customer = customerMapper.selectByExample(customerExample).get(0);

        //获取景点对象
        TravelSightsExample travelSightsExample = new TravelSightsExample();
        travelSightsExample.createCriteria().andTravelSightsNameEqualTo(travelName);
        TravelSights travelSights = travelSightsMapper.selectByExample(travelSightsExample).get(0);

        myorder.setCustId(customer.getCustId());
        myorder.setTravelSightsId(travelSights.getTravelSightsId());
        myorder.setItineraryPlanId(itineraryId);
        myorder.setNumberAdults(audltNum);
        myorder.setNumberChild(childNum);
        myorder.setMyorderDate(new Date());
        myorder.setMyorderStartDate(myorderStartDate);
        myorder.setMyorderPrice(myorderPrice);
        String myorderNo = UUID.randomUUID().toString().replace("-", "").substring(0,23);
        myorder.setMyorderNo(myorderNo);
        myorder.setMyorderStatus(0);    //所有订单创建状态都为未付款
        myorder.setStatus(0);
        myorderMapper.insert(myorder);

        MyorderExample myorderExample = new MyorderExample();
        myorderExample.createCriteria().andMyorderNoEqualTo(myorderNo);
        Myorder myorder1 = myorderMapper.selectByExample(myorderExample).get(0);


        return myorder1;
    }

    @Override
    public boolean updateByMyorderNo(String orderNo) {
        MyorderExample myorderExample = new MyorderExample();
        myorderExample.createCriteria().andMyorderNoEqualTo(orderNo);
        Myorder myorder = myorderMapper.selectByExample(myorderExample).get(0);
        myorder.setMyorderStatus(1);
        int rows = myorderMapper.updateByPrimaryKey(myorder);
//        if (rows > 0){
//            return ServerResponse.updateSuccess(rows);
//        }ServerResponse.updateFail(rows)
        return rows > 0;
    }

    @Override
    public boolean updateByMyorderId(Integer orderId,Integer orderStatus) {
        Myorder myorder = myorderMapper.selectByPrimaryKey(orderId);
        myorder.setMyorderStatus(orderStatus);
        return myorderMapper.updateByPrimaryKey(myorder) > 0;
    }

    @Override
    public boolean createFlow(String myorderNo) {
        Flow flow = new Flow();
        MyorderExample myorderExample = new MyorderExample();
        myorderExample.createCriteria().andMyorderNoEqualTo(myorderNo);
        Myorder myorder = myorderMapper.selectByExample(myorderExample).get(0);
        flow.setMyorderNo(myorderNo);
        flow.setFlowNum(String.valueOf(new Date().getTime()));
        flow.setPaidAmount(myorder.getMyorderPrice());
        flow.setPaidTime(new Date());
        flow.setFlowStatus(0);
        return flowMapper.insert(flow) > 0;
    }

    @Override
    public boolean deleteMyorder(String myorderNo) {
        MyorderExample myorderExample = new MyorderExample();
        myorderExample.createCriteria().andMyorderNoEqualTo(myorderNo);
        Myorder myorder = myorderMapper.selectByExample(myorderExample).get(0);
        myorder.setStatus(1);
        int rows = myorderMapper.updateByPrimaryKey(myorder);
        return rows > 0;
    }

    @Override
    public boolean cancelMyorder(String myorderNo) {
        MyorderExample myorderExample = new MyorderExample();
        myorderExample.createCriteria().andMyorderNoEqualTo(myorderNo);
        Myorder myorder = myorderMapper.selectByExample(myorderExample).get(0);
        myorder.setMyorderStatus(3);
        int rows = myorderMapper.updateByPrimaryKey(myorder);
        return rows > 0;
    }

    @Override
    public ServerResponse getDayTickets(Date date,Integer sightId) {
        MyorderExample myorderExample = new MyorderExample();
        myorderExample.createCriteria().andMyorderStartDateEqualTo(date).andTravelSightsIdEqualTo(sightId);
        List<Myorder> myorders = myorderMapper.selectByExample(myorderExample);
        ArrayList<Integer> listNum = new ArrayList<>();
        for (Myorder myorder:myorders
             ) {
            listNum.add(myorder.getNumberAdults());
        }
        System.out.println(myorders.size());
        if (listNum.size()>0){
            return ServerResponse.getSuccess(listNum);
        }else {
            return ServerResponse.getFail(null);
        }
    }

    @Override
    public boolean addMyorderReview(String myorderNo,String content) {
        MyorderExample myorderExample = new MyorderExample();
        myorderExample.createCriteria().andMyorderNoEqualTo(myorderNo);
        Myorder myorder = myorderMapper.selectByExample(myorderExample).get(0);
        SightsReview sightsReview = new SightsReview();
        sightsReview.setCustId(myorder.getCustId());
        sightsReview.setTravelSightsId(myorder.getTravelSightsId());
        sightsReview.setSightsReviewStatus(0);
        sightsReview.setSightsReviewText(content);
        sightsReview.setSightsReviewTime(new Date());
        return sightsReviewMapper.insert(sightsReview)>0;

    }
    @Override
    public Myorder getMyOrder(String orderNo) {
        MyorderExample myorderExample = new MyorderExample();
        myorderExample.createCriteria().andMyorderNoEqualTo(orderNo);
        Myorder myorder = myorderMapper.selectByExample(myorderExample).get(0);
        return myorder;
    }
}
